home *** CD-ROM | disk | FTP | other *** search
/ Internet.Works 39 / Issue 39.iso / mac / MacSoftware / Netscape 6 Full Installer / Installer Modules / browser.xpi / viewer / chrome / toolkit.jar / content / global / helperAppLauncher.js < prev    next >
Encoding:
JavaScript  |  2000-09-29  |  6.7 KB  |  178 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released March
  14.  * 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape Communications
  17.  * Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  */
  23.  
  24. function nsHelperAppLauncherDialog() {
  25.     // Initialize data properties.
  26.     this.userChoseApp = false;
  27.     this.chosenApp    = null;
  28.  
  29.     try {
  30.         // App launcher is passed as dialog argument.
  31.         this.appLauncher = window.arguments[0];
  32.  
  33.         // Initialize the dialog contents.
  34.         this.initDialog();
  35.     } catch( e ) {
  36.         // On error, close dialog.
  37.         dump( "nsHelperAppLauncherDialog error: " + e + "\n" );
  38.         window.close();
  39.     }
  40. }
  41.  
  42. nsHelperAppLauncherDialog.prototype= {
  43.     // Statics.
  44.     nsIHelperAppLauncher : Components.interfaces.nsIHelperAppLauncher,
  45.     nsIMIMEInfo             : Components.interfaces.nsIMIMEInfo,
  46.     nsIFilePicker        : Components.interfaces.nsIFilePicker,
  47.  
  48.     // Fill dialog from app launcher attributes.
  49.     initDialog : function () {
  50.         // "Always ask me" is always set (or else we wouldn't have got here!).
  51.         document.getElementById( "alwaysAskMe" ).checked = true;
  52.         document.getElementById( "alwaysAskMe" ).setAttribute( "disabled", "true" );
  53.  
  54.         // Pre-select the choice the user made last time.
  55.         if ( this.appLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.saveToDisk ) {
  56.             // Run app.
  57.             document.getElementById( "runApp" ).checked = true;
  58.  
  59.             this.chosenApp = this.appLauncher.MIMEInfo.preferredApplicationHandler;
  60.             var applicationDescription = this.appLauncher.MIMEInfo.applicationDescription;
  61.             if (applicationDescription != "")
  62.               document.getElementById( "appName" ).value = applicationDescription;
  63.             else
  64.                 // If a user-chosen application, show its path.
  65.                 document.getElementById( "appName" ).value = this.chosenApp.unicodePath;
  66.         } else {
  67.             // Save to disk.
  68.             document.getElementById( "saveToDisk" ).checked = true;
  69.             // Disable choose app button.
  70.             document.getElementById( "chooseApp" ).setAttribute( "disabled", "true" );
  71.         }
  72.  
  73.         // Put content type into dialog text.
  74.         var html = document.getElementById( "intro" );
  75.         if ( html && html.childNodes && html.childNodes.length ) {
  76.             // Get raw text.
  77.             var text = html.childNodes[ 0 ].nodeValue;
  78.             // Substitute content type for "#1".
  79.             text = text.replace( /#1/, this.appLauncher.MIMEInfo.MIMEType );
  80.  
  81.             // Replace #2 with product name.
  82.             var brandBundle = srGetStrBundle("chrome://global/locale/brand.properties");
  83.             if ( brandBundle ) {
  84.                 var product = brandBundle.GetStringFromName( "brandShortName" );
  85.                 text = text.replace( /#2/, product );
  86.             }
  87.  
  88.             // Replace text in document.
  89.             html.childNodes[ 0 ].nodeValue = text;
  90.         }
  91.  
  92.         // Set up dialog button callbacks.
  93.         var object = this;
  94.         doSetOKCancel( function () { return object.onOK(); },
  95.                        function () { return object.onCancel(); } );
  96.         moveToAlertPosition();
  97.     },
  98.  
  99.     // If the user presses OK, we do as requested...
  100.     onOK : function () {
  101.         // Get boolean switch from checkbox.
  102.         var dontAskNextTime = !document.getElementById( "alwaysAskMe" ).checked;
  103.     
  104.         if ( document.getElementById( "runApp" ).checked ) {
  105.             // Update preferred action if the user chose an app.
  106.             if ( this.userChoseApp ) {
  107.                 this.appLauncher.MIMEInfo.preferredAction = this.nsIHelperAppLauncher.useHelperApp;
  108.             }
  109.             this.appLauncher.launchWithApplication( this.chosenApp, dontAskNextTime );
  110.         } else {
  111.             this.appLauncher.MIMEInfo.preferredAction = this.nsIHelperAppLauncher.saveToDisk;
  112.             this.appLauncher.saveToDisk( null, dontAskNextTime );
  113.         }
  114.     
  115.         window.close();
  116.     },
  117.  
  118.     // If the user presses cancel, tell the app launcher and close the dialog...
  119.     onCancel : function () {
  120.         // Cancel app launcher.
  121.         try {
  122.             this.appLauncher.Cancel();
  123.         } catch( exception ) {
  124.         }
  125.     
  126.         // Close up dialog by returning true.
  127.         return true;
  128.     },
  129.  
  130.     // Enable pick app button if the user chooses that option.
  131.     toggleChoice : function () {
  132.         // See what option is checked.
  133.         if ( document.getElementById( "runApp" ).checked ) {
  134.             // We can enable the pick app button.
  135.             document.getElementById( "chooseApp" ).removeAttribute( "disabled" );
  136.         } else {
  137.             // We can disable the pick app button.
  138.             document.getElementById( "chooseApp" ).setAttribute( "disabled", "true" );
  139.         }
  140.     },
  141.  
  142.     // Choose a new/different app...
  143.     chooseApp : function () {
  144.         var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( this.nsIFilePicker );
  145.         fp.init( window,
  146.                  this.getString( "chooseAppFilePickerTitle" ),
  147.                  this.nsIFilePicker.modeOpen );
  148.         // XXX - We want to say nsIFilePicker.filterExecutable or something
  149.         fp.appendFilters( this.nsIFilePicker.filterAll );
  150.     
  151.         if ( fp.show() == this.nsIFilePicker.returnOK && fp.file ) {
  152.             // Remember the file they chose to run.
  153.             this.userChoseApp = true;
  154.             this.chosenApp    = fp.file;
  155.             // Update dialog.
  156.             document.getElementById( "appName" ).value = this.chosenApp.unicodePath;
  157.         }
  158.     },
  159.  
  160.     // Get string from bundle.
  161.     getString : function ( id ) {
  162.         // String of last resort is the id.
  163.         var result = id;
  164.     
  165.         // Get string bundle (if not done previously).
  166.         if ( !this.strings ) {
  167.             this.strings = srGetStrBundle("chrome://global/locale/helperAppLauncher.properties");
  168.         }
  169.     
  170.         if ( this.strings ) {
  171.             // Get string from bundle.
  172.             result = this.strings.GetStringFromName( id );
  173.         }
  174.     
  175.         return result;
  176.     },
  177. }
  178.